#include "common_log.h"
#include "ring_buffer.h"
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <thread>


void logger_test() {
    // 获取日志记录器单例实例
    rc::RCLogger& logger = rc::RCLogger::getInstance();

    // 添加一个简单的日志回调示例
    logger.addCallback([](const std::string& level, const std::string& message) {
        std::cout << "Callback received: Level [" << level << "], Message: " << message << std::endl;
    });

    // 使用不同日志级别记录日志示例
    logger.rs_trace("This is a trace message");
    logger.rs_debug("This is a debug message");
    logger.rs_info("This is an info message");
    logger.rs_warn("This is a warn message");
    logger.rs_error("This is an error message");
    logger.rs_critical("This is a critical message");

    // 设置日志级别示例（比如设置为只显示INFO及以上级别日志）
    logger.setLogLevel(rc::LogLevel::LOG_LEVEL_INFO);
}


void test_ringBuffer() {
    rc::RCLogger& logger = rc::RCLogger::getInstance();
    constexpr size_t Capacity = 10;
    constexpr size_t ImageSize = 1920 * 1080 * 2;

    rc::RingBuffer<uint8_t, Capacity, ImageSize> buffer;

    // sim image data
    uint8_t imageData[ImageSize] = {0};

    // producer
    std::thread producer([&] {
        for (int i = 0; i < 20; ++i) {
            if (buffer.tryPush(imageData, std::chrono::milliseconds(100))) {
                logger.rs_info(" Produced: {}", i);
            } else {
                logger.rs_error(" Produced timeout > 20");
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
    });

    // consumer
    std::thread consumer([&] {
        uint8_t outputData[ImageSize];
        for (int i = 0; i < 20; ++i) {
            if (buffer.tryPop(outputData, std::chrono::milliseconds(200))) {
                logger.rs_info(" Consumed {}", i);
            } else {
                logger.rs_error(" Consumed timeout > 100");
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(200));
        }
    });

    producer.join();
    consumer.join();
}


int main() {
    test_ringBuffer();

    return 0;
}
